﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TuyennvLibrary.Attributes;
using System.Reflection;
using System.Text.RegularExpressions;

namespace TuyennvLibrary.Reflection
{
    public class ApiController
    {
        private static DescriptionMethod getDescriptionMethod(Type myType, MethodInfo method, string appid, string urlapi)
        {
            string parameters = string.Empty;
            string parameterdev = string.Empty;
            string sParams = string.Empty;
            List<AttributeDescriptionMethod> listAttribute;
            DescriptionMethod description = new DescriptionMethod(method);
            AttributeDescriptionMethod[] attributes = (AttributeDescriptionMethod[])Attribute.GetCustomAttributes(method, typeof(AttributeDescriptionMethod));
            if (attributes != null && attributes.Length > 0)
            {
                listAttribute = new List<AttributeDescriptionMethod>();
                listAttribute.AddRange(attributes);
                listAttribute.Sort();
                foreach (AttributeDescriptionMethod attribute in listAttribute)
                {
                    description.Description += string.Format("{0}<br />", attribute.ToString());
                }

            }
            ParameterInfo[] parammeterArr = method.GetParameters();

            for (int i = 0; i < parammeterArr.Length; i++)
            {
                parameters += string.Format("{0} {1}{2}", parammeterArr[i].ParameterType.Name, parammeterArr[i].Name, (i < parammeterArr.Length - 1) ? ", " : "");
                if (!parammeterArr[i].Name.ToLower().Equals("appid"))
                {
                    parameterdev += string.Format("{0}=[{1}]{2}", parammeterArr[i].Name, parammeterArr[i].Name, (i < parammeterArr.Length - 1) ? "&" : "");
                }
                sParams += string.Format("{0}{1}", parammeterArr[i].Name, (i < parammeterArr.Length - 1) ? "," : "");
            }
            string request = string.Format("?dev=true&appid=" + appid + "&cmd={0}{1}{2}", method.Name, (string.IsNullOrEmpty(sParams)) ? "" : "&amp;params=" + sParams, (string.IsNullOrEmpty(parameterdev)) ? "" : "&" + parameterdev);
            description.Name = string.Format("<strong>{0}({1})</strong>", method.Name, parameters);
            description.LinkDev = urlapi + request;
            description.DeclaringType = method.GetBaseDefinition().DeclaringType;
            description.Inherit = !(description.DeclaringType.ToString() == myType.ToString());
            
            return description;
        }
        public static List<DescriptionMethod> getListDescriptionApi(string pathDll, string classApi, string appid, string urlapi)
        {
            string result = string.Empty;
            List<DescriptionMethod> listDescription = new List<DescriptionMethod>();
            //Xac dinh kieu cua class

            Type myTypeObj;
            try
            {
                if (!string.IsNullOrEmpty(pathDll))
                {
                    var dll = Assembly.LoadFile(pathDll);
                    myTypeObj = dll.GetType(classApi);
                }
                else
                {
                    myTypeObj = Type.GetType(classApi);
                }
                MethodInfo[] methods = myTypeObj.GetMethods();
                if (methods != null && methods.Length > 0)
                {
                    //    Array.Sort<MethodInfo>(methods,
                    //new Comparison<MethodInfo>(
                    //        (i1, i2) => i1.Name.CompareTo(i2.Name)
                    //));
                    foreach (MethodInfo method in methods)
                    {
                        if (method.ReturnType.Equals(typeof(object)))
                        {
                            try
                            {
                                DescriptionMethod obj = getDescriptionMethod(myTypeObj, method, appid, urlapi);
                                obj.Id = listDescription.Count;
                                listDescription.Add(obj);
                            }
                            catch (Exception)
                            {

                            }
                        }
                    }
                }

            }
            catch (Exception)
            {
            }
            return listDescription;
        }
        
    }
}
